package com.sdp.lease.config;

import com.sdp.core.bizc.interceptor.SystemInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;


@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

	@Value("${spring.resource.static-locations}")
	private String staticLocations;

	@Bean
	public CorsFilter corsFilter() {
		final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration corsConfiguration = new CorsConfiguration();
		/* 是否允许请求带有验证信息 */
		corsConfiguration.setAllowCredentials(true);
		/* 允许访问的客户端域名 */
		corsConfiguration.addAllowedOrigin("*");
		/* 允许服务端访问的客户端请求头 */
		corsConfiguration.addAllowedHeader("*");
		/* 允许访问的方法名,GET POST等 */
		corsConfiguration.addAllowedMethod("*");
		urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
		return new CorsFilter(urlBasedCorsConfigurationSource);
	}

	@Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大
        factory.setMaxRequestSize(DataSize.ofGigabytes(10)); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxFileSize(DataSize.ofGigabytes(30));
        return factory.createMultipartConfig();
    }

	/**
	 * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//取得绝对路径保证文件立马能读取
		String path = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\";
		registry.addResourceHandler("/**")
		.addResourceLocations(staticLocations.split(","))
		.addResourceLocations("file:\\"+path);
	}

	/**
	 * 方案一： 默认访问根路径跳转 doc.html页面 （swagger文档页面）
	 * 方案二： 访问根路径改成跳转 index.html页面 （简化部署方案： 可以把前端打包直接放到项目的 webapp，上面的配置）
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("doc.html");
	}

	/**
	 * 添加自定义拦截器,判断用户是否存在
	 * @return
	 */
	@Bean
	public SystemInterceptor systemInterceptor(){
		return new SystemInterceptor();
	}
	/**
	 * 拦截规则
	 */
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(systemInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/LoginController/login")
                .excludePathPatterns("/LoginController/generateKey")
                .excludePathPatterns("/LoginController/refreshToken")
                .excludePathPatterns("/SysUploadController/**")
				.excludePathPatterns("/onlyofficeController/**")
				.excludePathPatterns("/LeaseWeiXinController/**")
                .excludePathPatterns("/**/video/**")//存放视频位置s
                .excludePathPatterns("/**/*.jpeg")
		        .excludePathPatterns("/**/*.ico")
		        .excludePathPatterns("/**/*.png")
		        .excludePathPatterns("/**/*.jpg")
		        .excludePathPatterns("/**/*.pdf")
		        .excludePathPatterns("/**/*.svg")
		        .excludePathPatterns("/**/*.html")
		        .excludePathPatterns("/**/*.css")
		        .excludePathPatterns("/**/*.js")
		        .excludePathPatterns("/websocket/**")
		        .excludePathPatterns("/doc.html");

    }
}
